#!/usr/bin/env node

const http = require('https'),
      log  = console.log,
      url  = require('url'),
      cheerio = require('cheerio'),
      addr = 'https://trade.maoyan.com/films?showType=3';

let opt = url.parse(addr);
opt.headers = {
  'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36',
  'cookie':''
};


http.get(opt,(res) => {
  let data = '';

  res.on('data',(chunk) => data += chunk);
  res.on('end',() => {
    let $ = cheerio.load(data);
    let title = $('body').find('div.movie-item-title');
    let movieId = $('body').find('div.movie-item-title>a');
    let range = $('body').find('div.channel-detail-orange');

    var arr = [];
    for(let i = 0; i < title.length; i++){
      arr[i] = {
        movieId:$(movieId[i]).attr('href').split('/')[2],
        movieName:$(title[i]).attr('title'),
        movieRange:$(range[i]).text().trim()
      };
      log(arr[i]);
    }

  });
});
